package com.mingqijia.gassafety.webserver.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.DtuValveRecord;
import com.mingqijia.gassafety.db.mapper.DtuValveRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.SolenoidRecordRequest;
import com.mingqijia.gassafety.webserver.response.DtuValveRecordResponse;
import com.mingqijia.gassafety.webserver.service.DtuValveRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备、电磁阀操作记录表 服务实现类
 * </p>
 *
 * @author fei.zhang
 * @since 2023-08-23
 */
@Service
@Slf4j
public class DtuValveRecordServiceImpl extends ServiceImpl<DtuValveRecordMapper, DtuValveRecord> implements DtuValveRecordService {

    @Autowired
    private DtuValveRecordMapper dtuValveRecordMapper;
    /**
     * 根据imei和spid获取绑定记录
     * @param request
     * @param spId
     * @return
     */
    @Override
    public PageUtil<DtuValveRecordResponse> selectByImei(SolenoidRecordRequest request, String spId) {
        Page<DtuValveRecord> page = PageHelper.startPage(request.getPage(), request.getPageSize());
        List<DtuValveRecordResponse> dtuValveRecordResponses = new ArrayList<>();
        List<DtuValveRecord> dtuValveRecords = dtuValveRecordMapper.selectByImei(request.getImei(),spId);
        log.info("selectByImei-dtuValveRecords:{}", JSONObject.toJSONString(dtuValveRecords));
        if (!CollectionUtils.isEmpty(dtuValveRecords)){
            dtuValveRecords.forEach(dtuValveRecord ->{
                DtuValveRecordResponse dtuValveRecordResponse = new DtuValveRecordResponse();
                BeanUtils.copyProperties(dtuValveRecord,dtuValveRecordResponse);
                dtuValveRecordResponse.setEvent(dtuValveRecord.getEvent()==0?"解除关联":"关联");
                dtuValveRecordResponse.setOperationTime(DateUtil.format(dtuValveRecord.getCreatedAt(),"yyyy-MM-dd HH:mm:ss"));
                dtuValveRecordResponses.add(dtuValveRecordResponse);
            });
        }
        PageUtil pageInfo = new PageUtil<>(request.getPage(),
                request.getPageSize(), page.getTotal(), dtuValveRecordResponses);
        return pageInfo;
    }

    @Override
    public Boolean hasValve(String imei, String spId) {

        LambdaQueryWrapper<DtuValveRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DtuValveRecord::getDeviceId, imei).eq(DtuValveRecord::getEvent, Constants.BIND)
                .eq(DtuValveRecord::getSpId, spId).eq(DtuValveRecord::getIsDeleted, Constants.IS_DELETED_FALSE);
        List<DtuValveRecord> list = this.list(lqw);
        if (!CollectionUtils.isEmpty(list)){
            return true;
        }
        return false;
    }
}
